<template>
  <div style="display: flex;justify-content: center;">
    <el-card class="box-card" style="width: 60%;">
      <div slot="header" class="clearfix">
        <span>{{admin.remark}}</span>
      </div>
      <div>
        <div style="display: flex; justify-content: center;">
          <el-upload
            action="/admin/userFace"
            :headers="headers"
            :data="admin"
            :show-file-list="false"
            :on-success="onSuccess"
            >
            <img :src="admin.userFace" alt="" title="点击修改用户头像" style="width: 100px; height: 100px; border-radius: 50px;">
          </el-upload>
        </div>
        <div style="display: flex; justify-content: space-between;margin-top:40px;margin-bottom: 20px;">
          <div>账号名称：<el-tag>{{admin.name}}</el-tag></div>
          <div>电话号码：<el-tag>{{admin.telphone}}</el-tag></div>
          <div>手机号码：<el-tag>{{admin.phone}}</el-tag></div>
        </div>
        <div style="display: flex; justify-content: space-between;margin-bottom: 20px;">
          <div>居住地址：<el-tag>{{admin.address}}</el-tag></div>
        </div>
        <div style="margin-bottom: 40px;">用户标签：<el-tag v-for="(r,index) in admin.roles" :key="index" style="margin-right:5px;" type="success">{{r.remark}}</el-tag></div>
        <div style="display: flex; justify-content: center;">
          <el-button type="primary" @click="showEditView">修改信息</el-button>
          <el-button type="danger" @click="showEditPasswordView">修改密码</el-button>
        </div>
      </div>
    </el-card>
    <el-dialog
      title="修改用户信息"
      :visible.sync="dialogVisible"
      width="40%">
      <div>
        <table>
          <tr>
            <td>用户昵称：</td>
            <td><el-input v-model="editAdmin.remark"></el-input></td>
          </tr>
          <tr>
            <td>电话号码：</td>
            <td><el-input v-model="editAdmin.telphone"></el-input></td>
          </tr>
          <tr>
            <td>手机号码：</td>
            <td><el-input v-model="editAdmin.phone"></el-input></td>
          </tr>
          <tr>
            <td>用户地址：</td>
            <td><el-input v-model="editAdmin.address"></el-input></td>
          </tr>
        </table>
      </div>
      <span slot="footer" class="dialog-footer">
        <el-button @click="dialogVisible = false">取 消</el-button>
        <el-button type="primary" @click="updateAdminInfo">确 定</el-button>
      </span>
    </el-dialog>
    <el-dialog
      title="修改用户密码"
      :visible.sync="psdDialogVisible"
      width="40%">
      <div>
        <el-form :model="ruleForm" status-icon :rules="rules" ref="ruleForm" label-width="100px" class="demo-ruleForm">
          <el-form-item label="请输入旧密码" prop="oldPass">
            <el-input type="password" v-model="ruleForm.oldPass" autocomplete="off"></el-input>
          </el-form-item>
          <el-form-item label="请输入新密码" prop="pass">
            <el-input type="password" v-model="ruleForm.pass" autocomplete="off"></el-input>
          </el-form-item>
          <el-form-item label="请确认新密码" prop="checkPass">
            <el-input type="password" v-model="ruleForm.checkPass" autocomplete="off"></el-input>
          </el-form-item>
          <el-form-item>
            <el-button type="primary" @click="submitForm('ruleForm')">提交</el-button>
            <el-button @click="resetForm('ruleForm')">重置</el-button>
          </el-form-item>
        </el-form>
      </div>
    </el-dialog>
  </div>
</template>

<script>
  export default {
    name: 'AdminInfo',
    data(){
      var validatePass = (rule, value, callback) => {
        if (value === '') {
          callback(new Error('请输入密码'));
        } else {
          if (this.ruleForm.checkPass !== '') {
            this.$refs.ruleForm.validateField('checkPass');
          }
          callback();
        }
      };
      var validatePass2 = (rule, value, callback) => {
        if (value === '') {
          callback(new Error('请再次输入密码'));
        } else if (value !== this.ruleForm.pass) {
          callback(new Error('两次输入密码不一致!'));
        } else {
          callback();
        }
      };
      var validateOldPass = (rule, value, callback) => {
        if (value === '') {
          callback(new Error('请输入旧密码'));
        } else {
          if (this.ruleForm.checkPass !== '') {
            this.$refs.ruleForm.validateField('oldPass');
          }
          callback();
        }
      };
      return {
        admin: {},
        editAdmin: {},
        dialogVisible: false,
        psdDialogVisible: false,
        ruleForm: {
          oldPass: '',
          pass: '',
          checkPass: ''
        },
        rules: {
          pass: [
            { validator: validatePass, trigger: 'blur' }
          ],
          checkPass: [
            { validator: validatePass2, trigger: 'blur' }
          ],
          oldPass: [
            { validator: validateOldPass, trigger: 'blur' }
          ]
        },
        headers: {
          Authorization: window.sessionStorage.getItem('tokenStr')
        }
      };
    },
    mounted(){
      this.initAdmin();
    },
    methods: {
      initAdmin(){
        this.getRequest('/login/getUserInfo').then(resp=>{
          if(resp){
            this.admin = resp.obj;
            this.editAdmin = Object.assign({}, this.admin)
            window.sessionStorage.setItem('user', JSON.stringify(resp.obj))
            this.$store.commit('INIT_ADMIN',resp.obj)
          }
        })
      },
      showEditView(){
        this.dialogVisible = true;
      },
      updateAdminInfo(){
        this.putRequest('/system/admin/',this.editAdmin).then(resp=>{
          if(resp){
            this.dialogVisible = false;
            this.initAdmin();
          }
        })
      },
      showEditPasswordView(){
        this.psdDialogVisible = true;
      },
      submitForm(formName) {
        this.$refs[formName].validate((valid) => {
          if (valid) {
            this.ruleForm.adminId = this.admin.id;
            this.putRequest('/admin/pass', this.ruleForm).then(resp=>{
              if(resp){
                // 更新密码成功后
                this.postRequest('/login/doLogout');
                window.sessionStorage.removeItem('user');
                window.sessionStorage.removeItem('tokenStr');
                this.$store.commit('initRoutes',[]);
                this.$router.replace('/')
              }
            })
          } else {
            console.log('error submit!!');
            return false;
          }
        });
      },
      resetForm(formName) {
        this.$refs[formName].resetFields();
      },
      onSuccess(){
        console.log(111);
        this.initAdmin();
      }
    }
  }
</script>

<style scoped>
</style>